using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLNhaSach.BUS;
using QLNhaSach.BUS.DTO;

namespace QLNhaSach.GUI.NhanVienQuanLy
{
    public partial class ThemThongTinKhachHang : Form
    {
        DataTable dt = new DataTable();
        KhachHangDTO kh = new KhachHangDTO();

        public ThemThongTinKhachHang()
        {
            InitializeComponent();
        }

        private void ClearForm()
        {
            txtTenKH.Clear();
            txtDienThoai.Clear();
            txtDiaChi.Clear();
            txtTenKH.Focus();
        }

        private void ThemThongTinKhachHang_Load(object sender, EventArgs e)
        {
            dt = NhanVienQuanLyBUS.DSKH();
            lvDSKH.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem item = new ListViewItem();
                item.Text = dr["TenKH"].ToString();
                //item.SubItems.Add(dr["NgaySinh"].ToString());
                item.SubItems.Add(dr["DienThoai"].ToString());
                item.SubItems.Add(dr["DiaChi"].ToString());
                lvDSKH.Items.Add(item);
            }
        }

        private void lvDSKH_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = lvDSKH.FocusedItem.Index;
            if (index != -1)
            {
                DataRow dr = dt.Rows[index];
                txtTenKH.Text = dr["TenKH"].ToString();
                txtDiaChi.Text = dr["DiaChi"].ToString();
                txtDienThoai.Text = dr["DienThoai"].ToString();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtTenKH.Text != null && txtDienThoai.Text != null && txtDiaChi.Text != null)
            {
                KhachHangDTO kh = new KhachHangDTO();
                kh.TenKH = txtTenKH.Text;
                kh.DiaChi = txtDiaChi.Text;
                kh.DienThoai = txtDienThoai.Text;
                if (NhanVienQuanLyBUS.ThemKH(kh) == NhanVienQuanLySachBUSResult.DAOERROR)
                {
                    MessageBox.Show("Loi CSDL Khong The Tao Moi !");
                }
                else
                {
                    ClearForm();
                    MessageBox.Show("Them Khach Hang Thanh Cong !!");
                    ThemThongTinKhachHang_Load(sender, e);
                }
            }
            else
            {
                MessageBox.Show("Thong Tin Khong Day Du, Khong The Tao Moi !");
            }
            
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            int index = lvDSKH.FocusedItem.Index;

            KhachHangDTO kh = new KhachHangDTO();
            kh.MaKH = Convert.ToInt32( dt.Rows[index][0]);
            kh.TenKH = txtTenKH.Text;
            kh.DiaChi = txtDiaChi.Text;
            kh.DienThoai = txtDienThoai.Text;
            if (NhanVienQuanLyBUS.CapNhatKH(kh) == NhanVienQuanLySachBUSResult.DAOERROR)
            {
                MessageBox.Show("Loi CSDL Khong The Cap Nhat !");
            }
            else
            {
                ClearForm();
                ThemThongTinKhachHang_Load(sender, e);
                MessageBox.Show("Cap Nhat Khach Hang Thanh Cong !!");
            }
        }
    }
}